package com.jnc.web.biz.system.controller;

import com.jnc.common.base.BaseResp;
import com.jnc.core.base.controller.BaseController;
import com.jnc.core.web.page.TableResult;
import com.jnc.web.biz.system.model.DictType;
import com.jnc.web.biz.system.service.DictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jjn
 * @Date: 2018/11/7
 * @Desc:
 */
@Slf4j
@Controller
@RequestMapping("/system/dictType")
public class DictTypeController extends BaseController {
    private String prefix = "system/dictType";

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 初始化展示页面
     * @return
     */
    @RequiresPermissions("system:dict:view")
    @GetMapping()
    public String init(){
        return prefix + "/list";
    }

    /**
     * 初始化新增页面
     * @param model
     * @return
     */
    @GetMapping("/addInit")
    public String addInit(ModelMap model){
        return prefix + "/add";
    }

    /**
     * 初始化编辑页面
     * @param dictId
     * @param model
     * @return
     */
    @GetMapping("/editInit/{dictId}")
    public String editInit(@PathVariable("dictId") Integer dictId, ModelMap model){
        model.put("info", dictTypeService.getDictType(dictId));
        return prefix + "/edit";
    }

    /**
     * 查询列表数据
     * @param dictType
     * @return
     */
    @RequiresPermissions("system:dict:list")
    @PostMapping("/list")
    @ResponseBody
    public TableResult list(DictType dictType){
        log.debug("DictTypeController.list.dictType : {}", dictType);
        initPage();
        List<DictType> list = dictTypeService.queryDictType(dictType);
        return getTableData(list);
    }

    /**
     * 新增
     * @param dictType
     * @return
     */
    @RequiresPermissions("system:dictType:add")
    @PostMapping("/add")
    @ResponseBody
    public BaseResp add(@RequestBody DictType dictType){
        log.debug("DictTypeController.add.dictType : {}", dictType);
        //校验字典类型是否已存在
        List<DictType> list = dictTypeService.checkDictType(dictType);
        if(list != null && list.size() > 0){
            return error("该字典类型已存在");
        }
        return toAjax(dictTypeService.addDictType(dictType));
    }

    /**
     * 编辑
     * @param dictType
     * @return
     */
    @RequiresPermissions("system:dictType:edit")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResp edit(@RequestBody DictType dictType){
        log.debug("DictTypeController.edit.dictType : {}", dictType);
        //校验字典类型是否已存在
        List<DictType> list = dictTypeService.checkDictType(dictType);
        if(list != null && list.size() > 0){
            return error("该字典类型已存在");
        }
        return toAjax(dictTypeService.editDictType(dictType));
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequiresPermissions("system:dictType:remove")
    @PostMapping("/remove")
    @ResponseBody
    public BaseResp remove(@RequestParam String ids){
        log.debug("DictTypeController.delete.ids : {}", ids);
        return toAjax(dictTypeService.deleteDictType(ids));
    }

    /**
     * 查询字典详情列表
     * @param dictId
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{dictId}")
    public String detail(@PathVariable("dictId") Integer dictId, ModelMap mmap){
        mmap.put("dict", dictTypeService.getDictType(dictId));
        mmap.put("dictList", dictTypeService.getAllDictType());
        return "system/dictData/list";
    }
}
